package com.organization

import grails.test.*
import java.text.SimpleDateFormat

class DepartmentIntegrationTests extends GrailsUnitTestCase {
    protected void setUp() {
        super.setUp()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testSave() {
        SimpleDateFormat sdfSource = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdfSource.parse("2012-06-27 00:00:00");
        def dep = new Department(name:"BU IP",dateCreated: null)
		assertNotNull dep.save()
		assertNotNull dep.id

        dep.dateCreated = date
        assertNotNull dep.save()

		def foundDep = Department.get(dep.id)
		assertEquals "BU IP", foundDep.name
        assertEquals "Wed Jun 27 00:00:00 MSK 2012", foundDep.dateCreated.toString()
    }

	void testSaveAndUpdate() {
		def dep = new Department(name:"BU WAC")
		assertNotNull dep.save()

		def foundDep = Department.get(dep.id)
        assertNotNull foundDep.id

		foundDep.name = "BU IP"
		foundDep.save()
		def editedDep = Department.get(dep.id)
		assertEquals "BU IP", editedDep.name
	}
	
	void testSaveThenDelete() {
		def dep = new Department(name:"BU WAC")
		assertNotNull dep.save()

		def foundDep = Department.get(dep.id)
		foundDep.delete()
		assertFalse Department.exists(foundDep.id)
	}

	void testEvilSave() {
		def dep = new Department(name:"B")
		assertFalse dep.validate()
		assertTrue dep.hasErrors()
		assertNull dep.save()

		def errors = dep.errors
		assertEquals "size.toosmall",errors.getFieldError("name").code
		assertEquals "B",errors.getFieldError("name").rejectedValue

		dep.name = "BU IP"
		assertTrue(dep.validate())
		assertFalse(dep.hasErrors())
		assertNotNull dep.save()	
	}
}
